#!/bin/bash

ifeq (,$(TOP_DIR))
TOP_DIR := $(shell pwd)/../..
CURRENT_DIR := $(shell pwd)
else
CURRENT_DIR := $(shell pwd)
endif

QTSM_SDK_SRC = $(shell find $(CURRENT_DIR)/../source -name '*.c')
QTSM_SAM_SRC = $(shell find $(CURRENT_DIR)/src -name '*.c')

QTSM_SDK_OBJ = $(QTSM_SDK_SRC:.c=.o)
QTSM_SAM_OBJ = $(QTSM_SAM_SRC:.c=.o)

INCLUDE_PATH += -I $(CURRENT_DIR)/../include
INCLUDE_PATH += -I /usr/local/include/cjson
INCLUDE_PATH += -I /usr/include/glib-2.0/
INCLUDE_PATH += -I /usr/lib/x86_64-linux-gnu/glib-2.0/include
INCLUDE_PATH += -I /usr/lib64/glib-2.0/include

QTSM_SAM_INC := -I $(CURRENT_DIR)/include

LIB := -lssl -lcrypto
LIB += -lcurl
LIB += -ljson-c
LIB += -lqtsm
LIB += -lpthread
LIB += -lglib-2.0
LIB += -lsecurec
LIB += -leSDKOBS

QTSM_LIB_PATH += -L $(CURRENT_DIR)/lib
QTSM_LIB_PATH += -L /usr/bin
QTSM_LIB_PATH += -L /usr/lib
QTSM_LIB_PATH += -L /lib

ifeq (,$(CC))
CC := gcc
endif

GCC_CHOOSE := -W -Os -g -DSDK_DEBUG
GCC_CHOOSE += -Wl,-z,relro,-z,now
GCC_CHOOSE += -Wl,-z,noexecstack
GCC_CHOOSE += -fstack-protector-strong

enclave_demo_bin:$(QTSM_SAM_OBJ) $(QTSM_SDK_OBJ)
	$(CC) $(GCC_CHOOSE) $(INC) -o $@ $(QTSM_SDK_OBJ) $(QTSM_SAM_OBJ) $(QTSM_LIB_PATH) $(LIB)

$(QTSM_SDK_OBJ): %.o: %.c
	$(CC) -c $(GCC_CHOOSE) $< -o $@ $(INCLUDE_PATH)

$(QTSM_SAM_OBJ): %.o: %.c
	$(CC) -c $(GCC_CHOOSE) $< -o $@ $(INCLUDE_PATH) $(QTSM_SAM_INC)

clean:
	rm enclave_demo_bin $(QTSM_SDK_OBJ) $(QTSM_SAM_OBJ) -rf